<?php

namespace app\club\controller;

use app\common\controller\Clubend;

use think\Controller;
use think\Request;
use think\Session;
use think\Db;
use app\club\model\CUser;
use fast\Http;
use think\Config;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Gameuser extends Clubend
{
    
    /**
     * User模型对象
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Userinfo');
        $this->assign('title','添加成员');

    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
     * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    
    /**
     * 查看
     */
    public function index(){
        //当前俱乐部信息
        $clubid = (int)$this->request->param('clubid');
        if(empty($clubid)){
            $clubid = Session::get('clubid');
        }else{
            Session::set("clubid", $clubid);
        }
        if($clubid <= 0){
            return $this->redirect('/club/club/index');
        }
        
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name'))
            {
                return $this->selectpage();
            }
            //没有查询条件时不显示任何玩家
            $filter = $this->request->get("filter", '');
            $filter_arr = json_decode($filter,true);
            if(empty($filter_arr)){
                $result = array("total" => 0, "rows" => []);
                return json($result);
            }
            $uid_arr = $filter_arr['uid'];
            $uid_arr = explode(',',$uid_arr);
            if(count($uid_arr) > 20){
                $this->error('搜索数量过多');
            }
            $map = [];
            $map['uid'] = array('in',$uid_arr);
            
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            
            $total = $this->model
            ->where($map)
            ->order($sort, $order)
            ->count();
        
            $list = $this->model
            ->where($map)
            ->order($sort, $order)
            ->limit($offset, $limit)
            ->select();
            
            //处理数据
            $map['clubid'] = $clubid;
            $club_user_list = Db::table('fa_c_user')->where($map)->column('uid');
            foreach ($list as $key=>$val){
                $list[$key]['clubid'] = $clubid;
                $list[$key]['in_club'] = '未加入';//没在当前俱乐部
                if(in_array($val['uid'], $club_user_list)){
                    $list[$key]['in_club'] = '已加入';//在当前俱乐部
                }
            }
            
            $result = array("total" => $total, "rows" => $list);
        
            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 邀请加入俱乐部
     * @param number $clubid
     * @param number $uid
     */
    public function add($clubid = 0,$uid = 0){
        //基础判断
        if (!$this->request->isPost()){
            $this->error('请正确操作');
        }
        if(empty($clubid) || empty($uid)){
            $this->error('请选择要执行的信息');
        }
        
        $usercount = Db::table('fa_c_user')->where(['clubid'=>$clubid])->count();
        if($usercount >= 500){
            $this->error('俱乐部人员已满');
        }
        $club_user = Db::table('fa_c_user')->where(['clubid'=>$clubid,'uid'=>$uid])->find();
        if(!empty($club_user)){
            $this->error('已经是该俱乐部成员');
        }
        
        //游戏服务器
        $sreq = [
            'id' => (int)$clubid,
            'uid' => (int)$uid,
        ];
         
        //发送给游戏服务器
        $res = Http::get(Config::get('GameDB.api_url').'clubInvite',$sreq);
        if($res != 'ok')
        {
            $this->error($res);
        }
        $this->success('已成功邀请');
    }

}
